package com.example.restatr.Atcivity.Traffic;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.example.restatr.Atcivity.Main.MainActivity;
import com.example.restatr.Atcivity.Traffic.Bean.TrafficPatenteBean;
import com.example.restatr.R;
import com.example.restatr.Tools;
import com.google.gson.Gson;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

public class PatenteAt extends AppCompatActivity {
    private Toolbar tb;
    private TextView barTitle;
    private EditText search;
    private RelativeLayout card;
    private TextView licenseNo;
    private TextView licenseLevel;
    private TextView score;
    private TextView idCard;
    private TextView auditOffice;
    private TextView applyDate;
    private TextView verifyDate;
    private LinearLayout zsLl;
    private ImageView zsIcon;
    private TextView zsText;
    private List<TrafficPatenteBean.DataBean> dataBean =new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate( savedInstanceState );
        setContentView( R.layout.activity_patente_at );
        tb = findViewById( R.id.toolbar);
        barTitle = findViewById( R.id.bar_Title );
        search = findViewById( R.id.search );
        card = findViewById( R.id.card );
        licenseNo = findViewById( R.id.licenseNo );
        licenseLevel = findViewById( R.id.licenseLevel );
        score = findViewById( R.id.score );
        idCard = findViewById( R.id.idCard );
        auditOffice = findViewById( R.id.auditOffice );
        applyDate = findViewById( R.id.applyDate );
        verifyDate = findViewById( R.id.verifyDate );
        zsLl = findViewById( R.id.zs_ll );
        zsIcon = findViewById( R.id.zs_icon );
        zsText = findViewById( R.id.zs_text );

        Tools.SetBarTitle( tb, barTitle, "驾驶证信息", search, this ,false);
        zsLl.setOnClickListener( new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                switch (zsText.getText().toString()) {
                    case "绑定驾驶证":
                        startActivity( new Intent(PatenteAt.this,BingDingPatenteAt.class) );
                        break;
                    case "解绑驾驶证":
                        DeleteLicense();
                        break;
                }
            }
        } );
    }

    //获取驾驶证信息
    private void GetLicenseInfo() {
        new Thread( () -> {
            String token = Tools.Get( "/prod-api/api/traffic/license/user", MainActivity.sp.getString( "token", "" ) );
            dataBean = new Gson().fromJson( token, TrafficPatenteBean.class ).getData();
            runOnUiThread( () -> {
                if (dataBean.size()>0) {
                    card.setVisibility( View.VISIBLE );
                    licenseNo.setText( dataBean.get( 0 ).getLicenseNo() );
                    licenseLevel.setText( "准驾车型：" + dataBean.get( 0 ).getLicenseLevel() );
                    score.setText( "驾驶分：" + dataBean.get( 0 ).getScore() + "分" );
                    idCard.setText( "身份证号：" + dataBean.get( 0 ).getIdCard() );
                    auditOffice.setText( "发证机关：" + dataBean.get( 0 ).getAuditOffice() );
                    applyDate.setText( "申领日期：" + dataBean.get( 0 ).getApplyDate() );
                    verifyDate.setText( "到期时间：" + dataBean.get( 0 ).getVerifyDate() );
                    zsIcon.setBackgroundResource( R.drawable.ic_round_remove_24 );
                    zsText.setText( "解绑驾驶证" );
                } else {
                    card.setVisibility( View.GONE );
                    zsIcon.setBackgroundResource( R.drawable.ic_round_add_24 );
                    zsText.setText( "绑定驾驶证" );
                }
            } );
        } ).start();
    }

    //解绑驾驶证
    private void DeleteLicense() {
        new Thread( () -> {
            try {
                String token = Tools.Delete( "/prod-api/api/traffic/license/" + dataBean.get( 0 ).getId(), MainActivity.sp.getString( "token", "" ) );
                JSONObject jx = new JSONObject( token );
                String msg = jx.getString( "msg" );
                int code = jx.getInt( "code" );
                runOnUiThread( () -> {
                    Toast.makeText( this, msg, Toast.LENGTH_SHORT ).show();
                    if (code==200){
                        GetLicenseInfo();
                    }
                } );
            } catch (JSONException e) {
                e.printStackTrace();
            }
        } ).start();
    }

    @Override
    protected void onResume() {
        GetLicenseInfo();
        super.onResume();
    }
}